﻿namespace HEFramework
{
    /// <summary>
    ///
    /// 单例接口
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2023-11-29 13:57:39
    /// ----------------------------------------
    /// </summary>
    public abstract class ASingleton : DisposeObject
    {
        internal abstract void Register();
    }

    /// <summary>
    ///
    /// 单例抽象类
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2023-11-29 13:57:58
    /// ----------------------------------------
    /// </summary>
    public abstract class Singleton<T> : ASingleton where T : Singleton<T>
    {
        [StaticField] private static T instance;

        [StaticField]
        public static T Instance
        {
            get { return instance; }
            private set { instance = value; }
        }

        private bool isDisposed;


        internal override void Register()
        {
            Instance = (T)this;
        }

        public override void Dispose()
        {
            if (this.isDisposed)
            {
                return;
            }

            this.isDisposed = true;
            this.Destroy();
            Instance = null;
        }

        public bool IsDisposed()
        {
            return this.isDisposed;
        }

        protected virtual void Destroy()
        {
        }
    }
}